<template>
  <div class="container">
    <div class="donghua">
      <ParticleAnimation></ParticleAnimation>
    </div>
    <div class="box">
      <div class="avatar">
        <img src="../imgs/tx.PNG" alt="">
      </div>
      <!-- 表单 -->
      <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="账号" prop="username">
          <el-input v-model="ruleForm.username" type="text" autocomplete="off" class="el-input" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" type="password" autocomplete="off" class="el-input" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)">Submit</el-button>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import ParticleAnimation from '../components/ParticleAnimation.vue'
import { useRoute, useRouter } from 'vue-router'
import axios from '@/utils/Request';
const router = useRouter()
const route = useRoute()
const ruleFormRef = ref<FormInstance>()
const username = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('Please input the Username'))
  } else {

    callback()
  }
}
const password = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('Please input the password again'))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  username: '',
  password: '',

})

const rules = reactive<FormRules<typeof ruleForm>>({
  username: [{ validator: username, trigger: 'blur' }],
  password: [{ validator: password, trigger: 'blur' }],

})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      console.log('submit!')
      const res = await axios.post('/api/login', {
        ...ruleForm
      }
      )
      console.log(res, "插入登陆成功！！！！");
      //const res = await axios.get('/api/login', //api/get 接口输错 和后端请求方式不一致 404
      if (res.data.code === 0) {
        //储存token   
        sessionStorage.setItem('token', res.data.token)
        sessionStorage.setItem('users', JSON.stringify(res.data.users))
        router.push('/home')
      }

    } else {
      console.log('error submit!')
      return false
    }
  })
}
</script>
<style scoped>
* {
  margin: 0;
  padding: 0;
  list-style: none;
}

.container {
  width: 100vw;
  height: 100vh;
  background-color: #5A99A1;
  position: relative;
  left: 0;
  top: 0;

}

.box {
  width: 40%;
  height: 300px;
  background-color: #CCCCCC;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

}

.box img {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  display: block;

}

.box .avatar {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  margin: -50px auto 50px;
  border: 1px solid #fff;
  padding: 2px;
}

.el-form-item {
  width: 30vw;
  margin-left: 13%;
  box-sizing: border-box;
}

.el-button {
  width: 10vw;
  height: 5vh;
  margin-left: 13%;
  box-sizing: border-box;
}
</style>